<?php

namespace Restaurant\Controller;


use Core\Config;
use Core\Constant;
use Core\Controller\RestaurantAdminBaseController;
use Core\Exception\BusinessException;
use Core\Helper\CommonHelper;
use Core\Helper\HtmlPurifierHelper;
use Core\Session\FrontSessionHelper;
use Exception;
use Zend\View\Model\JsonModel;

class DayDishController extends RestaurantAdminBaseController {
    public function indexAction() {
        $this->initPageWithLayout();
        return $this->getViewModel();
    }


    public function editDayDishAction() {
        try{
            $this->initPageWithLayout();
            $day = $this->params()->fromRoute('day');

            $daysInWeek = Config::getDayInWeek(FrontSessionHelper::getCurrentLang());
            if(!array_key_exists($day, $daysInWeek))
                throw new BusinessException('Ngày '.$day.' không hợp lệ.');
            $mlCategoryTable = $this->getResTableFinder()->getMlCategoryTable(
                FrontSessionHelper::getCurrentLang());
            $allMainCategories = $mlCategoryTable->getAllCategoriesByGroupCode(Constant::$CATEGORY_GROUP_DISH);
            unset($allMainCategories[0]);
            $this->getViewModel()->setVariable('allMainCategories', $allMainCategories);

            $this->getViewModel()->setVariable('day', $daysInWeek[$day]);
            $this->getViewModel()->setVariable('day_code', $day);






            return $this->getViewModel();
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_DISTRIBUTION_EXCEPTION);
            return $this->genErrorView($e);
        }
    }

    public function saveAction(){
        try{
            $day = $this->params()->fromPost('day');
            $dishIdArr = $this->params()->fromPost('dishIdArr');
            $dishIdArr = array_unique($dishIdArr);

            $dayDishTable = $this->getResTableFinder()->
                getDayDishTable();
            $transProxy = $this->getTableGatewayTransactionProxy();
            $transProxy->setAbstractTable($dayDishTable);
            $dayDishTable->saveDayDish($day, $dishIdArr);

            return new JsonModel(array('success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_DISTRIBUTION_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }


}